package src
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import gs.TweenMax;

	public class NavegacionGaleria extends Sprite
	{
		private var _cont;
		
		public function NavegacionGaleria()
		{
			this.visible = false;
			this.mouseChildren = true;
		}
		
		public function init ():void
		{
			_cont = this.getChildByName('paginacion');
			_cont.y += 2;
			mascara.width = (_cont.getChildAt(0).width + 5)*3;
			_cont.mask = mascara;
			
			if(_cont.numChildren > 3){
				nav.izq.alpha = 0.4;
				nav.der.addEventListener(MouseEvent, clicD);
			}else{
				nav.visible = false;
			}
			
			this.visible = true;
		}
		
		private function clicD (e:MouseEvent):void
		{
			nav.der.removeEventListener(MouseEvent, clicD);
			nav.izq.addEventListener(MouseEvent, clicI);
			TweenMax.to(nav.der, 0.2, {alpha:0.4});
			TweenMax.to(nav.izq, 0.2, {alpha:1});
			TweenMax.to(_cont, 0.3, {x:_cont.x - mascara.width, onComplete:compruebaDerecha});
		}
		
		private function compruebaDerecha ():void
		{
			if(_cont.x <= (_cont.width + mascara.width)){
				_cont.x = _cont.width + mascara.width;
				nav.der.buttonMode = false;
			}else{
				nav.der.addEventListener(MouseEvent, clicD);
			}
		}
		
		private function clicI (e:MouseEvent):void
		{
			nav.izq.removeEventListener(MouseEvent, clicD);
			nav.der.addEventListener(MouseEvent, clicI);
			TweenMax.to(nav.izq, 0.2, {alpha:0.4});
			TweenMax.to(nav.der, 0.2, {alpha:1});
			TweenMax.to(_cont, 0.3, {x:_cont.x + mascara.width, onComplete:compruebaIzq});
		}
		
		private function compruebaIzq ():void
		{
			if(_cont.x >= 0){
				_cont.x = 0;	
				nav.izq.buttonMode = false;
			}else{
				nav.izq.addEventListener(MouseEvent, clicI);
			}
				}
		
		
	}
}